import { Controller } from 'egg';

class WorkUnitController extends Controller {
    /**
     * 创建单位
     */
    public async createWorkUnit() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                work_unit_name: 'string',
                admin_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.workUnitService.createWorkUnit(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除单位
     */
    public async deleteWorkUnit() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                work_unit_id: 'string',
                admin_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.workUnitService.deleteWorkUnit(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找单位
     */
    public async searchWorkUnit() {
        const { ctx } = this;
        const result = await ctx.service.workUnitService.searchWorkUnit();
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据单位id查找单位信息
     */
    public async searchWorkUnitById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                work_unit_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.workUnitService.searchWorkUnitById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改单位
     */
    public async updateWorkUnit() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                work_unit_id: 'string',
                work_unit_name:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.workUnitService.updateWorkUnit(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }
}
export default WorkUnitController;